package com.cmc6.user.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cmc6.common.util.ApiResult;
import com.cmc6.request.model.entity.RequestDO;
import com.cmc6.user.model.dto.*;
import com.cmc6.user.model.vo.UserCenterBaseInfoVO;
import com.cmc6.user.service.UserCenterService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;

@RestController
@RequestMapping(value = "/userCenter")
@Api(tags = "用户中心")
public class UserCenterController {

    @Resource
    UserCenterService baseService;

    @ApiOperation(value = "用户基本信息")
    @PostMapping(value = "/baseInfo")
    public ApiResult<UserCenterBaseInfoVO> baseInfo() {
        return ApiResult.ok(baseService.baseInfo());
    }

    @ApiOperation(value = "修改用户基本信息")
    @PostMapping(value = "/updateBaseInfo")
    public ApiResult<String> updateBaseInfo(@RequestBody @Valid UserCenterUpdateBaseInfoDTO dto) {
        return ApiResult.ok(baseService.updateBaseInfo(dto));
    }

    @ApiOperation(value = "刷新用户 jwt私钥后缀")
    @PostMapping(value = "/refreshJwtSecretSuf")
    public ApiResult<String> refreshJwtSecretSuf() {
        return ApiResult.ok(baseService.refreshJwtSecretSuf());
    }

    @ApiOperation(value = "修改密码")
    @PostMapping(value = "/updatePassword")
    public ApiResult<String> updatePassword(@RequestBody @Valid UserCenterUpdatePasswordDTO dto) {
        return ApiResult.ok(baseService.updatePassword(dto));
    }

    @ApiOperation(value = "修改邮箱")
    @PostMapping(value = "/updateEmail")
    public ApiResult<String> updateEmail(@RequestBody @Valid UserCenterUpdateEmailDTO dto) {
        return ApiResult.ok(baseService.updateEmail(dto));
    }

    @ApiOperation(value = "邮箱修改：发送验证码")
    @PostMapping(value = "/updateEmail/sendCode")
    public ApiResult<String> updateEmailSendCode(@RequestBody @Valid EmailNotBlankDTO dto) {
        return ApiResult.ok(baseService.updateEmailSendCode(dto));
    }

    @ApiOperation(value = "邮箱绑定：发送验证码")
    @PostMapping(value = "/bindEmail/sendCode")
    public ApiResult<String> bindEmailSendCode(@RequestBody @Valid EmailNotBlankDTO dto) {
        return ApiResult.ok(baseService.bindEmailSendCode(dto));
    }

    @ApiOperation(value = "修改手机号")
    @PostMapping(value = "/updatePhone")
    public ApiResult<String> updatePhone(@RequestBody @Valid UserCenterUpdatePhoneDTO dto) {
        return ApiResult.ok(baseService.updatePhone(dto));
    }

    @ApiOperation(value = "修改手机号：发送验证码")
    @PostMapping(value = "/updatePhone/sendCode")
    public ApiResult<String> updatePhoneSendCode(@RequestBody @Valid PhoneNotBlankDTO dto) {
        return ApiResult.ok(baseService.updatePhoneSendCode(dto));
    }

    @ApiOperation(value = "手机绑定：发送验证码")
    @PostMapping(value = "/bindPhone/sendCode")
    public ApiResult<String> bindPhoneSendCode(@RequestBody @Valid PhoneNotBlankDTO dto) {
        return ApiResult.ok(baseService.bindPhoneSendCode(dto));
    }

    @ApiOperation(value = "查看登录记录")
    @PostMapping(value = "/loginRecord")
    public ApiResult<Page<RequestDO>> loginRecord(@RequestBody @Valid UserCenterLoginRecordDTO dto) {
        return ApiResult.ok(baseService.loginRecord(dto));
    }

    @ApiOperation(value = "账号注销")
    @PostMapping(value = "/delAccount")
    public ApiResult<String> delAccount(@RequestBody @Valid UserCenterDelAccountDTO dto) {
        return ApiResult.ok(baseService.delAccount(dto));
    }

    @ApiOperation(value = "退出登录")
    @PostMapping(value = "/logout")
    public ApiResult<String> logout() {
        return ApiResult.ok(baseService.logout());
    }

}
